import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders


class SmtTool:
    def __init__(self, receiver):
        # 设置SMTP服务器地址、登录信息等参数
        self.smtp_server = 'smtp.exmail.qq.com'
        self.username = 'wuxl@thinksonic.cn'
        self.password = 'Long1995'
        self.sender = self.username
        # self.receiver = ['wuxl@thinksonic.cn']
        self.receiver = receiver
        self.subject = '发布资料'

    def send_email(self, file_path):
        try:
            # 创建MIME文本对象并添加内容
            msg = MIMEMultipart()
            msg['Subject'] = '发布资料'
            msg['From'] = self.sender
            msg['To'] = ', '.join(self.receiver)

            body = 'This is a test email with an attachment.'
            msg.attach(MIMEText(body, 'plain'))

            # 添加附件
            attachment_file_path = file_path
            with open(attachment_file_path, 'rb') as file:
                attachment = MIMEBase('application', 'octet-stream')
                attachment.set_payload(file.read())
                encoders.encode_base64(attachment)
                attachment.add_header('Content-Disposition',
                                      'attachment; filename="{}"'.format(attachment_file_path.split('/')[-1]))
                msg.attach(attachment)

            # 连接到SMTP服务器并发送邮件
            server = smtplib.SMTP(self.smtp_server)
            server.login(self.username, self.password)
            server.sendmail(self.sender, self.receiver, msg.as_string())
        except Exception as e:
            pass
        finally:
            server.quit()
            os.remove(file_path)


if __name__ == '__main__':
    c = "a"
    s = SmtTool()
    s.send_email()
